#!/bin/sh
########################################################################
# setup.sh - gmould installation script
# Copyright (C) 2013 Mohamad Afifi Omar <afifio@gmail.com>
#
# These comments cannot be remove or edited and should remain intact
# because this file comments is an extension to the GPL3 license.
# This software file is covered by GPL version 3 license.
# Please see the LICENSE and gpl3.txt or gpl3.htm in doc directory
# for the license agreement, if its not exist, write to the 
# Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
# Boston, MA 02110-1301, USA.
# Modification below this line is allowed
########################################################################
# Extra credits to:
#
########################################################################
# Notes:
# 
########################################################################
function linking()
{
	rm -rf $2
	ln -s $1 $2
}
#XDG_CACHE_HOME=/root/.cache
#XDG_CONFIG_DIRS=/etc/xdg
#XDG_CONFIG_HOME=/root/.config
#XDG_CURRENT_DESKTOP=LXDE
#XDG_DATA_DIRS=/usr/share:/usr/local/share
#XDG_DATA_HOME=/root/.local/share
#XDG_MENU_PREFIX=lxde-
function GetFirstOrder()
{
	#cannot have IFS redefined in the wide env
	#it will screw the running path.
	#possibly ifs not restored correctly. - afifio
	local ifssaved=$IFS; IFS=":";
	FirstOrder $1
	IFS=$ifsaved
};export -f GetFirstOrder
function FirstOrder()
{
	echo $1
};export -f FirstOrder
echo ----------------------------------- [ gmould Setup ]
ROOT_UID=0             # Root has $UID 0.
CP="cp --update --recursive "
LINKING=linking
# "echo linking... "$1" to "$2"; ln -s"
MKDIR="mkdir -p"

if [ "$UID" -ne "$ROOT_UID" ]
then
	if [ -n "$XDG_DATA_HOME" ]
	then
		INSTALLPATH=`GetFirstOrder $XDG_DATA_HOME`/gmould
	else
		INSTALLPATH=$HOME/.local/share/gmould
	fi
	echo "User specific installation into " $INSTALLPATH
elif [ "$UID" = "$ROOT_UID" ]
then
	if [ -n "$XDG_DATA_DIRS" ]
	then
		INSTALLPATH=`GetFirstOrder $XDG_DATA_DIRS`/gmould
	else
		INSTALLPATH=/usr/share/gmould
	fi
	echo "Systemwide installation into " $INSTALLPATH
fi
	#  installation
	PATHNOW=`pwd`
	CONFIGPATH=$HOME/.config/gmould
	echo "Start copying the executable"
	$CP gmould /usr/bin/gmould
	$LINKING /usr/bin/gmould /usr/local/bin/gmould
	#$CP gmouldnewproject /usr/bin/gmouldnewproject
	$LINKING /usr/bin/gmould /usr/local/bin/gmouldnewproject
	
	echo "Creating the shared install dirs..."
	$MKDIR $INSTALLPATH
	$MKDIR $INSTALLPATH/src
	$MKDIR $INSTALLPATH/lib
	$MKDIR $INSTALLPATH/pix
	$MKDIR $INSTALLPATH/dlg
	$MKDIR $INSTALLPATH/doc
	$MKDIR $INSTALLPATH/locale
	$MKDIR $INSTALLPATH/template
	$MKDIR $INSTALLPATH/data
	$MKDIR /usr/local/apps/gmould
	
	echo "Copying the files..."	
	$CP $PATHNOW/bin/ $INSTALLPATH
	$CP $PATHNOW/data/ $INSTALLPATH
	$CP $PATHNOW/lib/ $INSTALLPATH
	$CP $PATHNOW/pix/ $INSTALLPATH
	$CP $PATHNOW/dlg/ $INSTALLPATH
	$CP $PATHNOW/doc/ $INSTALLPATH
	$CP $PATHNOW/locale/ $INSTALLPATH
	$CP $PATHNOW/pix/ $INSTALLPATH
	$CP $PATHNOW/src/ $INSTALLPATH
	$CP $PATHNOW/template $INSTALLPATH
	echo "gMould support files..."
	$CP $PATHNOW/pix/dist/*.* /usr/share/icons/
		
	
	echo "Desktop system awareness..."
	$CP $PATHNOW/bin/AppRun /usr/local/apps/gmould/AppRun
	$CP $PATHNOW/bin/AppInfo.xml /usr/local/apps/gmould/AppInfo.xml
	$CP $PATHNOW/bin/gmould.desktop /usr/share/applications/gmould.desktop
	$LINKING $INSTALLPATH/pix/gmould.svg /usr/share/icons/gmould.svg
	$LINKING $INSTALLPATH/pix/gmould.svg /usr/share/icons/hicolor/48x48/apps/gmould.svg
	$LINKING $INSTALLPATH/pix/gmould.svg /usr/local/apps/gmould/.DirIcon
	$LINKING $INSTALLPATH/pix/gmould.svg /usr/local/lib/X11/pixmaps/gmould.svg
	$LINKING $INSTALLPATH/pix/gmould.svg /usr/share/pixmaps/gmould.svg
	$LINKING $INSTALLPATH/doc /usr/share/doc/gmould
	echo "User config and runtime files..."
	$MKDIR $CONFIGPATH
	$MKDIR $CONFIGPATH/tmp
echo 0 > $CONFIGPATH/tmp/preference
echo "Type here what you want, use it as a notice/scratch board" > $CONFIGPATH/tmp/notepad.txt
echo "#!/bin/bash" > $CONFIGPATH/tmp/script.sh
echo "<span color=\"Navy\" font-family=\"Sans\" size=\"large\"><i>Mohamad Afifi Omar</i></span>" > $CONFIGPATH/tmp/textpad.txt
echo "gmould ready" > $CONFIGPATH/tmp/error
echo "gmould ready" > $CONFIGPATH/tmp/warning
echo "gmould ready" > $CONFIGPATH/tmp/question
echo "gmould ready" > $CONFIGPATH/tmp/info
echo "gmould ready" > $CONFIGPATH/tmp/authentication
echo "gActionNothing" > $CONFIGPATH/tmp/SignalParameter
echo "variable" > $CONFIGPATH/tmp/ConditionParameter
echo gtk-apply " | "`date +%X` "| gmould installation done| gmould Setup" > $CONFIGPATH/tmp/log
echo "gmould now installed, executing..."
gmould
